//
//  SINAPhotoPopView.m
//  sinashebao
//
//  Created by  androidlongs on 2017/12/28.
//  Copyright © 2017年  androidlongs. All rights reserved.
//

#import "SINAStuTargetFinishPopView.h"
#import "SINACircularBeadUtils.h"
#import "SINACalendarUtils.h"


@interface  SINAStuTargetFinishPopView()


@property(strong,nonatomic)UIImageView *titleImageView;

@property(strong,nonatomic) UILabel *savePhotoUILabel;
@property(strong,nonatomic) UILabel *userNikeNameUILabel;
@property(strong,nonatomic) UILabel *userFlagUILabel;
@property(strong,nonatomic) UIView *deleteView;
@property(strong,nonatomic) UILabel *targetEndTimeUILabel;
@property(strong,nonatomic) UILabel *targetEndTimeUILabel2;
@property(strong,nonatomic) UILabel *targetTitleUILabel;
@property(strong,nonatomic) UILabel *targetTitlleFlagUILabel;
@property(strong,nonatomic) UILabel *targetStatueLabel;

@property(strong,nonatomic) UILabel *targetUseTimeLabel;
@property(strong,nonatomic) UILabel *targetUseTotalLabel;

@property(strong,nonatomic)UIImageView *finishImageView;

@end
@implementation SINAStuTargetFinishPopView


- (instancetype)init
{
    self = [super init];
    if (self) {
        
        
        self.bounds = [UIScreen mainScreen].bounds;
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.94];
        self.layer.opacity = 0.0;
        
//        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disMiss)];
        self.userInteractionEnabled = YES;
       // [self addGestureRecognizer:tap];
        
        [self addSubview:self.contentView];
        
        self.contentView.frame = CGRectMake(40, self.contentView.frame.origin.y, mainW-40*2, 430);
        [SINACircularBeadUtils makeCircularBead:10 andView:self.contentView andIsSetBackground:NO];
        
        UIImageView *bgImageView = [[UIImageView alloc]init];
        bgImageView.frame =CGRectMake(0, 0,CGRectGetWidth(self.contentView.frame), CGRectGetHeight(self.contentView.frame));
        [self.contentView addSubview:bgImageView];
        bgImageView.image =[UIImage imageNamed:@"target_finish_bg"];
        
        
        
        UIImageView *titleImageView = [[UIImageView alloc]init];
        titleImageView.frame =CGRectMake(26, 36,64,64);
        [self.contentView addSubview:titleImageView];
        titleImageView.image =[UIImage imageNamed:@"默认头像"];
        [SINACircularBeadUtils makeCircularView:titleImageView];
        self.titleImageView=titleImageView;
        
        
        
        UILabel *userNikeNameUILabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.titleImageView.frame)+16,36+10, CGRectGetWidth(self.contentView.frame)-CGRectGetMaxX(self.titleImageView.frame)-10-16, 21)];
        userNikeNameUILabel.text=@"-- --";
        userNikeNameUILabel.font=[UIFont systemFontOfSize:16];
        userNikeNameUILabel.textColor=[UIColor colorWithHexString:@"#212121"];
        self.userNikeNameUILabel=userNikeNameUILabel;
        [self.contentView addSubview:userNikeNameUILabel];
        
        
        UILabel *userFlagUILabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.titleImageView.frame)+16,CGRectGetMaxY(userNikeNameUILabel.frame)+4, CGRectGetWidth(self.contentView.frame)-CGRectGetMaxX(self.titleImageView.frame)-10-16, 21)];
        userFlagUILabel.text=@"-- --";
        userFlagUILabel.font=[UIFont systemFontOfSize:12];
        userFlagUILabel.textColor=[UIColor colorWithHexString:@"#2e2e2e"];
        self.userFlagUILabel=userFlagUILabel;
        [self.contentView addSubview:userFlagUILabel];
        
        UIView *deleteView = [[UIView alloc]init];
        deleteView.frame=CGRectMake(CGRectGetWidth(self.contentView.frame)-100, 0, 100,42);
        self.deleteView = deleteView;
        [self.contentView addSubview:deleteView];

        [[SINAUiviewClickLiserner alloc] viewClickFunction:deleteView andWithBlick:^(UIView *view) {
            [self disMiss];
        }];
        
        
        UIImageView *closeImageView = [[UIImageView alloc]init];
        closeImageView.frame =CGRectMake(CGRectGetWidth(deleteView.frame)-13-14, CGRectGetHeight(deleteView.frame)-10-10,13,13);
        [self.deleteView addSubview:closeImageView];
        closeImageView.image =[UIImage imageNamed:@"target_pop_close_icon"];
        
        
        
        UILabel *targetEndTimeUILabel = [[UILabel alloc]initWithFrame:CGRectMake(15,CGRectGetMaxY(titleImageView.frame)+26, CGRectGetWidth(self.contentView.frame)-30, 21)];
        targetEndTimeUILabel.text=@"星期 -- --月--日";
        targetEndTimeUILabel.font=[UIFont systemFontOfSize:12];
        targetEndTimeUILabel.textColor=[UIColor colorWithHexString:@"#878787"];
        self.targetEndTimeUILabel=targetEndTimeUILabel;
        [self.contentView addSubview:targetEndTimeUILabel];
        
        UILabel *targetEndTimeUILabel2 = [[UILabel alloc]initWithFrame:CGRectMake(15,CGRectGetMaxY(targetEndTimeUILabel.frame)-2, CGRectGetWidth(self.contentView.frame)-30, 21)];
        targetEndTimeUILabel2.text=@"--:--:--";
        targetEndTimeUILabel2.font=[UIFont systemFontOfSize:12];
        targetEndTimeUILabel2.textColor=[UIColor colorWithHexString:@"#878787"];
        self.targetEndTimeUILabel2=targetEndTimeUILabel2;
        [self.contentView addSubview:targetEndTimeUILabel2];
        
        
        UIImageView *finishImageView = [[UIImageView alloc]init];
        finishImageView.frame =CGRectMake(50, CGRectGetMaxY(targetEndTimeUILabel2.frame)+30,64,67);
        [self.contentView addSubview:finishImageView];
        finishImageView.image =[UIImage imageNamed:@"target_no_finish_icon"];
        self.finishImageView =finishImageView;
        
        UILabel *targetTitleUILabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(finishImageView.frame)+30,CGRectGetMaxY(targetEndTimeUILabel2.frame)+40, CGRectGetWidth(self.contentView.frame)-30, 21)];
        targetTitleUILabel.text=@"跑步";
        targetTitleUILabel.font=[UIFont systemFontOfSize:16];
        targetTitleUILabel.textColor=baseSelectColor;
        self.targetTitleUILabel=targetTitleUILabel;
        [self.contentView addSubview:targetTitleUILabel];
        
        UILabel *targetTitlleFlagUILabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(finishImageView.frame)+30,CGRectGetMaxY(targetTitleUILabel.frame)+2, CGRectGetWidth(self.contentView.frame)-30, 21)];
        targetTitlleFlagUILabel.text=@"已达标";
        targetTitlleFlagUILabel.font=[UIFont systemFontOfSize:12];
        targetTitlleFlagUILabel.textColor=[UIColor colorWithHexString:@"#878787"];
        self.targetTitlleFlagUILabel=targetTitlleFlagUILabel;
        [self.contentView addSubview:targetTitlleFlagUILabel];
        
        
        UILabel *selectUILabel = [[UILabel alloc]initWithFrame:CGRectMake(15,CGRectGetHeight(self.contentView.frame)-15-32, CGRectGetWidth(self.contentView.frame)-30, 32)];
        selectUILabel.text=@"确定";
        selectUILabel.font=[UIFont systemFontOfSize:14];
        selectUILabel.textAlignment=NSTextAlignmentCenter;
        selectUILabel.backgroundColor = baseSelectColor;
        selectUILabel.textColor=[UIColor colorWithHexString:@"#ffffff"];
        
        [SINACircularBeadUtils makeCircularBead:16 andView:selectUILabel ];
       
        [self.contentView addSubview:selectUILabel];
        
        [[SINAUiviewClickLiserner alloc] viewClickFunction:selectUILabel andWithBlick:^(UIView *view) {
            [self disMiss];
        }];
        
        
        //目标
        UIView *flagTitleFooterView = [[UIView alloc]initWithFrame:CGRectMake(15, CGRectGetMinY(selectUILabel.frame)-76, (CGRectGetWidth(self.contentView.frame)-30)/3, 60)];
        flagTitleFooterView.backgroundColor=[UIColor clearColor];
        [self.contentView addSubview:flagTitleFooterView];
        
        UILabel *titleFlag1Label = [[UILabel alloc]initWithFrame:CGRectMake(15,0, CGRectGetWidth(flagTitleFooterView.frame)-15, 21)];
        titleFlag1Label.text=@"目标";
        titleFlag1Label.font=[UIFont systemFontOfSize:12];
        titleFlag1Label.textColor=[UIColor colorWithHexString:@"#000000"];
        [flagTitleFooterView addSubview:titleFlag1Label];
        
        UIView *diviFlag1View = [[UIView alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(titleFlag1Label.frame)+4, 15, 2)];
        diviFlag1View.backgroundColor=[UIColor blackColor];
        
        [flagTitleFooterView addSubview:diviFlag1View];
        
        UILabel *titleFlagValue1Label = [[UILabel alloc]initWithFrame:CGRectMake(15,CGRectGetMinY(diviFlag1View.frame)+6, CGRectGetWidth(flagTitleFooterView.frame)-15, 21)];
        titleFlagValue1Label.text=@"30min";
        titleFlagValue1Label.font=[UIFont systemFontOfSize:12];
        titleFlagValue1Label.textColor=[UIColor colorWithHexString:@"#000000"];
        [flagTitleFooterView addSubview:titleFlagValue1Label];
         self.targetUseTotalLabel =titleFlagValue1Label;
        
        
        //时长
        UIView *flagTimeFooterView = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(flagTitleFooterView.frame), CGRectGetMinY(selectUILabel.frame)-76, (CGRectGetWidth(self.contentView.frame)-30)/3, 60)];
        flagTimeFooterView.backgroundColor=[UIColor clearColor];
        [self.contentView addSubview:flagTimeFooterView];
        
        UILabel *titleFlag2Label = [[UILabel alloc]initWithFrame:CGRectMake(15,0, CGRectGetWidth(flagTitleFooterView.frame)-15, 21)];
        titleFlag2Label.text=@"时长";
        titleFlag2Label.font=[UIFont systemFontOfSize:12];
        titleFlag2Label.textColor=[UIColor colorWithHexString:@"#000000"];
        [flagTimeFooterView addSubview:titleFlag2Label];
        
        UIView *diviFlag2View = [[UIView alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(titleFlag2Label.frame)+4, 15, 2)];
        diviFlag2View.backgroundColor=[UIColor blackColor];
        
        [flagTimeFooterView addSubview:diviFlag2View];
        
        UILabel *titleFlagValue2Label = [[UILabel alloc]initWithFrame:CGRectMake(15,CGRectGetMinY(diviFlag2View.frame)+6, CGRectGetWidth(flagTitleFooterView.frame)-15, 21)];
        titleFlagValue2Label.text=@"00'00'00";
        titleFlagValue2Label.font=[UIFont systemFontOfSize:12];
        titleFlagValue2Label.textColor=[UIColor colorWithHexString:@"#000000"];
        [flagTimeFooterView addSubview:titleFlagValue2Label];
        self.targetUseTimeLabel =titleFlagValue2Label;
        
        //状态
        UIView *flagStatueFooterView = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(flagTimeFooterView.frame), CGRectGetMinY(selectUILabel.frame)-76, (CGRectGetWidth(self.contentView.frame)-30)/3, 60)];
        flagStatueFooterView.backgroundColor=[UIColor clearColor];
        [self.contentView addSubview:flagStatueFooterView];
        
        UILabel *titleFlag3Label = [[UILabel alloc]initWithFrame:CGRectMake(15,0, CGRectGetWidth(flagTitleFooterView.frame)-15, 21)];
        titleFlag3Label.text=@"状态";
        titleFlag3Label.font=[UIFont systemFontOfSize:12];
        titleFlag3Label.textColor=[UIColor colorWithHexString:@"#000000"];
        [flagStatueFooterView addSubview:titleFlag3Label];
        
        UIView *diviFlag3View = [[UIView alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(titleFlag3Label.frame)+4, 15, 2)];
        diviFlag3View.backgroundColor=[UIColor blackColor];
        
        [flagStatueFooterView addSubview:diviFlag3View];
        
        UILabel *titleFlagValue3Label = [[UILabel alloc]initWithFrame:CGRectMake(15,CGRectGetMinY(diviFlag3View.frame)+6, CGRectGetWidth(flagTitleFooterView.frame)-15, 21)];
        titleFlagValue3Label.text=@"已达标";
        titleFlagValue3Label.font=[UIFont systemFontOfSize:12];
        titleFlagValue3Label.textColor=[UIColor colorWithHexString:@"#000000"];
        [flagStatueFooterView addSubview:titleFlagValue3Label];
        self.targetStatueLabel = titleFlagValue3Label;
        
        
    }
    return self;
}





- (UIView *)contentView
{
    if (!_contentView) {
        _contentView = [[UIView alloc]initWithFrame:CGRectMake(0, mainH, mainW,200)];
        [_contentView setBackgroundColor:[UIColor clearColor]];
    }
    return _contentView;
}
//显示选择器
- (void)show
{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [self setCenter:[UIApplication sharedApplication].keyWindow.center];
    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
    
    
    self.deleteView.hidden=YES;
    
    CGRect frame =  self.contentView.frame;
    frame.origin.y = (mainH-self.contentView.frame.size.height)/2;
    [UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        [self.layer setOpacity:1.0];
        self.contentView.frame = frame;
        
    } completion:^(BOOL finished) {
        
        SINAUserModel *userModel = [[SINACommonUserHelper share]getUserModel];
        NSString *headerImageUrl = userModel.userTitleImageUrl;
        if (headerImageUrl!=nil&&headerImageUrl.length>1) {
            NSURL *imageUrl =[NSURL URLWithString:headerImageUrl];
            [self.titleImageView sd_setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"默认头像"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                DLog(@"加载头像完成");
            }];
        }
        
        
        NSString *userNikName = userModel.userNike;
        if (userNikName==nil||[userNikName isEqualToString:@""]) {
            NSString *phone = userModel.userPhone;
            if (phone==nil||[phone isEqualToString:@""]) {
                phone=@"设置昵称";
            }else{
                phone=@"设置昵称";
            }
            userNikName = phone;
        }
        self.userNikeNameUILabel.text=userNikName;
        
        NSString *sign =userModel.sign;
        if (sign==nil||sign.length==0||[sign isEqualToString:@"(null)"]) {
            sign=@"这个世界很美，不仿写个签名吧";
        }
        self.userFlagUILabel.text = sign;
        
        
        self.deleteView.hidden=NO;
        
        [self updateTimeFunction];
        
        
        NSString *statue=@"已成功";
        
        
        switch (self.targetStatue) {
            case 1:
                statue=@"已成功";
                self.finishImageView.image =[UIImage imageNamed:@"target_finish_icon"];
                break;
            case 2:
                 statue=@"已达标";
                self.finishImageView.image =[UIImage imageNamed:@"target_finish_icon"];
                break;
            case 3:
               statue= @"未达标";
                self.finishImageView.image =[UIImage imageNamed:@"target_no_finish_icon"];
                //target_no_finish_icon
                break;
            default:
                break;
        }
        self.targetStatueLabel.text = statue;
        self.targetUseTotalLabel.text=self.targetUseTotalTime;
        self.targetUseTimeLabel.text = self.targetUseTime;
        self.targetTitleUILabel.text = self.targetTitle;
        self.targetTitlleFlagUILabel.text=[NSString stringWithFormat:@"小目标%@",statue];
    }];
    
}

//移除选择器
- (void)disMiss
{
    
    CGRect frame =  self.contentView.frame;
    frame.origin.y = mainH;
    [UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        [self.layer setOpacity:0.0];
        self.contentView.frame = frame;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}


-(void)updateTimeFunction{
    if (self.targetEndTime!=nil) {
        
        NSString *week = [SINACalendarUtils dateWeekWithDateString:self.targetEndTime];
        NSString *dayTime = [SINACalendarUtils getDateStringWithTimeStr:self.targetEndTime andFormat:@"MM月dd日"];
        NSString *hourTime = [SINACalendarUtils getDateStringWithTimeStr:self.targetEndTime andFormat:@"HH:mm:ss"];
        self.targetEndTimeUILabel.text =[NSString stringWithFormat:@"%@ %@",week,dayTime];
        
        self.targetEndTimeUILabel2.text=hourTime;
    }
}
@end
